package com.szjlxh.data.repository;

import com.szjlxh.data.entity.WeatherPerHourBean;
import com.szjlxh.data.entity.WeatherPerSixHourBean;
import com.szjlxh.data.entity.WeatherSevenDayBean;
import com.szjlxh.data.entity.WeatherThreeDayForcastBean;
import com.szjlxh.data.entity.WeatherThreeHourBean;
import com.szjlxh.data.net.ApiConnection;
import com.szjlxh.data.net.ConstantUrl;
import com.szjlxh.data.utils.DataUtils;
import com.szjlxh.data.utils.GsonObjectFormatter;
import com.szjlxh.domain.model.WeatherPerHour;
import com.szjlxh.domain.model.index.WeatherSevenDay;
import com.szjlxh.domain.repository.IWeatherForcastRespository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by liutao on 16/3/19.
 */
public class WeatherForcastRespository implements IWeatherForcastRespository {

    @Inject
    public WeatherForcastRespository() {

    }

    @Override
    public Observable<List<WeatherPerHour>> weatherPerHour() {
        return Observable.create(new Observable.OnSubscribe<WeatherPerHourBean>() {
            @Override
            public void call(Subscriber<? super WeatherPerHourBean> subscriber) {
                String resString = null;
                try {
                    resString = ApiConnection.createGET(ConstantUrl.WEATHER_PER_HOUR).requestSyncCall();
                    subscriber.onNext(new GsonObjectFormatter<WeatherPerHourBean>(resString).getObjectFromType(WeatherPerHourBean.class));
                } catch (Exception e) {
                    subscriber.onError(e.getCause());
                }

            }
        }).map(new Func1<WeatherPerHourBean, List<WeatherPerHour>>() {
            @Override
            public List<WeatherPerHour> call(WeatherPerHourBean weatherPerHour) {
                List<WeatherPerHour> weatherPerHours = new ArrayList<>();
                for (int i = 0; i < weatherPerHour.getWEATHERDATA().size(); i++) {
                    WeatherPerHour temp = new WeatherPerHour();
                    temp.setDegree((int) weatherPerHour.getWEATHERDATA().get(i).getTEMP() + "°");
                    temp.setWeather(weatherPerHour.getWEATHERDATA().get(i).getWEATHERNAME());
                    temp.setWind(weatherPerHour.getWEATHERDATA().get(i).getWIND_DICRECTNAME() + "风");
                    temp.setWindLevel(weatherPerHour.getWEATHERDATA().get(i).getWIND_SCALE() + "级");
                    temp.setResString(weatherPerHour.getWEATHERDATA().get(i).getWEATHER());
                    String dateString = weatherPerHour.getWEATHERDATA().get(i).getBJ_DATETIME();
                    temp.setHour(DataUtils.getFormatHour(dateString));
                }

                return weatherPerHours;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }


    public Observable<List<WeatherPerHour>> weatherPerThreeHour() {
        return Observable.create(new Observable.OnSubscribe<WeatherThreeHourBean>() {
            @Override
            public void call(Subscriber<? super WeatherThreeHourBean> subscriber) {
                String resString = null;
                try {
                    resString = ApiConnection.createGET(ConstantUrl.WEATHER_THREE_HOUR).requestSyncCall();
                    subscriber.onNext(new GsonObjectFormatter<WeatherThreeHourBean>(resString).getObjectFromType(WeatherThreeHourBean.class));
                } catch (Exception e) {
                    subscriber.onError(e.getCause());
                }

            }
        }).map(new Func1<WeatherThreeHourBean, List<WeatherPerHour>>() {
            @Override
            public List<WeatherPerHour> call(WeatherThreeHourBean weatherPerHour) {
                List<WeatherPerHour> weatherPerHours = new ArrayList<>();
                for (int i = 0; i < weatherPerHour.getWeatherdata().size(); i++) {
                    WeatherPerHour temp = new WeatherPerHour();
                    temp.setDegree((int) weatherPerHour.getWeatherdata().get(i).getTemp() + "°");
                    temp.setWeather(weatherPerHour.getWeatherdata().get(i).getWeathername());
                    temp.setWind(weatherPerHour.getWeatherdata().get(i).getWind_dicrectname() + "风");
                    temp.setWindLevel(weatherPerHour.getWeatherdata().get(i).getWind_scale() + "级");
                    temp.setResString(weatherPerHour.getWeatherdata().get(i).getWeather());
                    String dateString = weatherPerHour.getWeatherdata().get(i).getBj_datetime();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    Date date = null;
                    try {
                        date = simpleDateFormat.parse(dateString);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    } finally {
                        if (date != null) {
                            temp.setHour(date.getHours() + "时");
                        }
                        weatherPerHours.add(temp);
                    }
                }

                return weatherPerHours;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<WeatherPerSixHourBean>> weatherPerSixHour() {
        return Observable.create(new Observable.OnSubscribe<List<WeatherPerSixHourBean>>() {
            @Override
            public void call(Subscriber<? super List<WeatherPerSixHourBean>> subscriber) {
                WeatherPerSixHourBean temp = new WeatherPerSixHourBean();
                temp.setResString("99");
                temp.setMonth("11/1");
                temp.setHourPeriod("08：00-14：00");
                temp.setWeather("多云");
                temp.setDegree("10/1℃");
                temp.setWindName("东北风");
                temp.setWindScale("3级");
                temp.setRain("0");
                List<WeatherPerSixHourBean> da = new ArrayList<>();
                for (int i = 0; i <= 12; i++) {
                    da.add(temp);
                }
                subscriber.onNext(da);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<WeatherSevenDay>> weatherSevenDay() {
        return Observable.create(new Observable.OnSubscribe<WeatherSevenDayBean>() {
            @Override
            public void call(Subscriber<? super WeatherSevenDayBean> subscriber) {
                String resString = null;
                try {
                    resString = ApiConnection.createGET(ConstantUrl.SEVEN_DAY_WEATHER_URL).requestSyncCall();
                    subscriber.onNext(new GsonObjectFormatter<WeatherSevenDayBean>(resString).getObjectFromType(WeatherSevenDayBean.class));
                } catch (Exception e) {
                    subscriber.onError(e.getCause());
                }
            }
        }).map(new Func1<WeatherSevenDayBean, List<WeatherSevenDay>>() {
            @Override
            public List<WeatherSevenDay> call(WeatherSevenDayBean weatherSevenDayBean) {
                List<WeatherSevenDay> weatherSevenDays = new ArrayList<>();
                for (int i = 0; i < weatherSevenDayBean.getData().size(); i++) {
                    WeatherSevenDayBean.DataEntity content = weatherSevenDayBean.getData().get(i);
                    WeatherSevenDay temp = new WeatherSevenDay();
                    temp.setIconLastName(content.getWeatherICON());
                    temp.setWeekDay(content.getWeekday());
                    temp.setMonthDay(content.getMonth() + "/" + content.getDay());
                    temp.setMaxTemp(Integer.valueOf(content.getTempMax()));
                    temp.setMingTemp(Integer.valueOf(content.getTempMin()));
                    temp.setWeatherName(content.getWeather());
                    temp.setWindName(content.getWindB());
                    temp.setWindScale(content.getWind());
                    weatherSevenDays.add(temp);
                }

                return weatherSevenDays;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<WeatherThreeDayForcastBean> weatherThreeDayForcast() {
        return Observable.create(new Observable.OnSubscribe<WeatherThreeDayForcastBean>() {
            @Override
            public void call(Subscriber<? super WeatherThreeDayForcastBean> subscriber) {
                String resString = null;
                try {
                    resString = ApiConnection.createGET(ConstantUrl.WEATHER_THREE_DAY_FORCAST).requestSyncCall();
                    subscriber.onNext(new GsonObjectFormatter<WeatherThreeDayForcastBean>(resString).getObjectFromType(WeatherThreeDayForcastBean.class));
                } catch (Exception e) {
                    subscriber.onError(e.getCause());
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
}
